<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Admin\Model\ArticleModel;

class ArticleController extends BaseController {

    /**
     * 分类
     */
    public function categoryList() {
        $ArticleMod = new ArticleModel();
        $catRs = $ArticleMod->articleCatList(0, 0, false);
        $type_arr = array('系统默认', '系统帮助', '系统公告');
        $this->assign('type_arr', $type_arr);
        $this->assign('cat_list', $catRs);
        $this->display('categoryList');
    }

    /**
     * 分类
     */
    public function category() {
        $ArticleMod = new ArticleModel();
        $act = I('GET.act', 'add');
        $this->assign('act', $act);
        $catId = I('GET.cat_id');
        $catRs = array();
        if ($catId) {
            $catRs = D('article_cat')->where('cat_id=' . $catId)->find();
            $this->assign('cat_info', $catRs);
        }
        $cats = $ArticleMod->articleCatList(0, $catRs['parent_id'], true);
        $this->assign('cat_select', $cats);
        $this->display();
    }

    /**
     * 文章列表
     */
    public function articleList() {
        $articleMod = M('Article');
        $res = $list = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];

        $where = " 1 = 1 ";
        $keywords = trim(I('keywords'));
        $keywords && $where .= " and title like '%$keywords%' ";
        $catId = I('cat_id', 0);
        $catId && $where .= " and cat_id = $catId ";
        $res = $articleMod->where($where)->order('article_id desc')->page("$p,$size")->select();
        $count = $articleMod->where($where)->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();

        $ArticleModel = new ArticleModel();
        $catRs = $ArticleModel->articleCatList(0, 0, false);
        if ($res) {
            foreach ($res as $val) {
                $val['category'] = $catRs[$val['cat_id']]['cat_name'];
                $val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
                $list[] = $val;
            }
        }
        $this->assign('cats', $catRs);
        $this->assign('cat_id', $catId);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    /**
     * 文章
     */
    public function article() {
        $ArticleMod = new ArticleModel();
        $act = I('GET.act', 'add');
        $arr = array();
        $arr['publish_time'] = time() + 3600 * 24;
        if (I('GET.article_id')) {
            $article_id = I('GET.article_id');
            $arr = D('article')->where('article_id=' . $article_id)->find();
        }
        $catRs = $ArticleMod->articleCatList(0, $arr['cat_id']);
        $this->assign('cat_select', $catRs);
        $this->assign('act', $act);
        $this->assign('info', $arr);
        $this->initEditor();
        $this->display();
    }

    /**
     * 初始化编辑器链接
     */
    private function initEditor() {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => 'article')));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => 'article')));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => 'article')));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => 'article')));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => 'article')));
        $this->assign("URL_Home", "");
    }

    /**
     * 分类操作
     */
    public function categoryHandle() {
        $post = I('post.');
        if ($post['act'] == 'add') {
            $status = D('article_cat')->add($post);
        }

        if ($post['act'] == 'edit') {
            if ($post['cat_id'] == $post['parent_id']) {
                $this->error("所选分类的上级分类不能是当前分类", U('Admin/Article/category', array('cat_id' => $post['cat_id'])));
            }
            $ArticleMod = new ArticleModel();
            $children = array_keys($ArticleMod->articleCatList($post['cat_id'], 0, false)); // 获得当前分类的所有下级分类
            if (in_array($post['parent_id'], $children)) {
                $this->error("所选分类的上级分类不能是当前分类的子分类", U('Admin/Article/category', array('cat_id' => $post['cat_id'])));
            }
            $status = D('article_cat')->where("cat_id=$post[cat_id]")->save($post);
        }

        if ($post['act'] == 'del') {
            $res = D('article_cat')->where('parent_id =' . $post['cat_id'])->select();
            if ($res) {
                exit(json_encode('还有子分类，不能删除'));
            }
            $res = D('article')->where('cat_id =' . $post['cat_id'])->select();
            if ($res) {
                exit(json_encode('非空的分类不允许删除'));
            }
            $rs = D('article_cat')->where('cat_id=' . $post['cat_id'])->delete();
            if ($rs)
                exit(json_encode(1));
        }
        if ($status) {
            $this->success("操作成功", U('Admin/Article/categoryList'));
        } else {
            $this->error("操作失败", U('Admin/Article/categoryList'));
        }
    }

    /**
     * 文章删除改
     */
    public function aticleHandle() {
        $post = I('post.');
        $post['publish_time'] = strtotime($post['publish_time']);
        //$post['content'] = htmlspecialchars(stripslashes($_POST['content']));
        if ($post['act'] == 'add') {
            unset($post['act'], $post['__hash__']);
            $post['click'] = mt_rand(1000, 1300);
            $post['add_time'] = time();
            $status = D('article')->add($post);
        }

        if ($post['act'] == 'edit') {
            unset($post['act'], $post['__hash__']);
            $status = D('article')->where('article_id=' . $post['article_id'])->save($post);
        }

        if ($post['act'] == 'del') {
            $status = D('article')->where('article_id=' . $post['article_id'])->delete();
            if ($status)
                exit(json_encode(1));
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Article/articleList');
        if ($status) {
            $this->success("操作成功", $referurl);
        } else {
            $this->error("操作失败", $referurl);
        }
    }

    /**
     * 友情链接
     */
    public function link() {
        $act = I('GET.act', 'add');
        $this->assign('act', $act);
        $linkId = I('GET.link_id');
        $linkRs = array();
        if ($linkId) {
            $linkRs = D('friend_link')->where('link_id=' . $linkId)->find();
            $this->assign('info', $linkRs);
        }
        $this->display();
    }

    /**
     * 友情链接列表
     */
    public function linkList() {
        $friendLinkMod = M('friend_link');
        $keywords = I('post.keywords');
        $where = '1=1';
        if (!empty($keywords)) {
            $where .= " and (link_name like '%{$keywords}%' )";
        }
        $res = $friendLinkMod->where($where)->order('orderby')->page($_GET['p'] . ',10')->select();
        if ($res) {
            foreach ($res as $val) {
                $val['target'] = $val['target'] > 0 ? '开启' : '关闭';
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $count = $friendLinkMod->where($where)->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('keywords', $keywords);
        $this->display();
    }

    /**
     * 友情连接增删改操作
     */
    public function linkHandle() {
        $post = I('post.');
        if ($post['act'] == 'add') {
            stream_context_set_default(array('http' => array('timeout' => 2)));
            send_http_status('311');
            $status = D('friend_link')->add($post);
        }
        if ($post['act'] == 'edit') {
            $status = D('friend_link')->where('link_id=' . $post['link_id'])->save($post);
        }
        if ($post['act'] == 'del') {
            $status = D('friend_link')->where('link_id=' . $post['link_id'])->delete();
            if ($status)
                exit(json_encode(1));
        }
        if ($status) {
            $this->success("操作成功", U('Admin/Article/linkList'));
        } else {
            $this->error("操作失败", U('Admin/Article/linkList'));
        }
    }

}
